package org.dxy.trigger.http;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.api.dto.request.product.*;
import org.dxy.api.dto.response.R;
import org.dxy.infrastructure.persistent.po.query.ProductViewPo;
import org.dxy.trigger.http.service.ProductApplicationService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.dxy.api.dto.vo.ProductVo;

/**
 * 产品控制器
 * @author dxy
 * @createTime 2025/8/17
 */
@Slf4j
@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
public class ProductController {

    private final ProductApplicationService productApplicationService;

    /**
     * 创建产品
     */
    @PostMapping
    public R<Long> create(@Validated @RequestBody CreateProductRequest request) {
        log.info("创建产品请求: {}", request);
        return productApplicationService.create(request);
    }

    /**
     * 更新产品
     */
    @PutMapping("/{productId}")
    public R<Void> update(@PathVariable Long productId, 
                         @Validated @RequestBody UpdateProductRequest request) {
        log.info("更新产品请求: productId={}, request={}", productId, request);
        return productApplicationService.update(productId, request);
    }

    /**
     * 启用产品
     */
    @PostMapping("/{productId}/enable")
    public R<Void> enable(@PathVariable Long productId) {
        log.info("启用产品请求: productId={}", productId);
        return productApplicationService.enable(productId);
    }

    /**
     * 禁用产品
     */
    @PostMapping("/{productId}/disable")
    public R<Void> disable(@PathVariable Long productId) {
        log.info("禁用产品请求: productId={}", productId);
        return productApplicationService.disable(productId);
    }

    /**
     * 删除产品
     */
    @DeleteMapping("/{productId}")
    public R<Void> delete(@PathVariable Long productId) {
        log.info("删除产品请求: productId={}", productId);
        return productApplicationService.delete(productId);
    }

    /**
     * 变更产品版本
     */
    @PostMapping("/{productId}/change-version")
    public R<Void> changeVersion(@PathVariable Long productId,
                                @Validated @RequestBody ChangeProductVersionRequest request) {
        log.info("变更产品版本请求: productId={}, request={}", productId, request);
        return productApplicationService.changeVersion(productId, request);
    }

    /**
     * 更新产品BOM
     */
    @PostMapping("/{productId}/update-bom")
    public R<Void> updateBOM(@PathVariable Long productId) {
        log.info("更新产品BOM请求: productId={}", productId);
        return productApplicationService.updateBOM(productId);
    }

    /**
     * 分配工艺路线
     */
    @PostMapping("/{productId}/assign-route")
    public R<Void> assignRoute(@PathVariable Long productId,
                              @Validated @RequestBody AssignProductRouteRequest request) {
        log.info("分配工艺路线请求: productId={}, request={}", productId, request);
        return productApplicationService.assignRoute(productId, request);
    }

    /**
     * 解绑工艺路线
     */
    @DeleteMapping("/{productId}/routes/{routeId}")
    public R<Void> unassignRoute(@PathVariable Long productId, @PathVariable Long routeId) {
        log.info("解绑工艺路线请求: productId={}, routeId={}", productId, routeId);
        return productApplicationService.unassignRoute(productId, routeId);
    }

    /**
     * 审核通过
     */
    @PostMapping("/{productId}/approve")
    public R<Void> approve(@PathVariable Long productId) {
        log.info("审核通过产品请求: productId={}", productId);
        return productApplicationService.approve(productId);
    }

    /**
     * 审核驳回
     */
    @PostMapping("/{productId}/reject")
    public R<Void> reject(@PathVariable Long productId,
                         @Validated @RequestBody RejectProductRequest request) {
        log.info("审核驳回产品请求: productId={}, request={}", productId, request);
        return productApplicationService.reject(productId, request);
    }

    /**
     * 分页查询产品列表
     */
    @GetMapping
    public R<Page<ProductViewPo>> list(@RequestParam(defaultValue = "1") int current,
                                       @RequestParam(defaultValue = "10") int size,
                                       ProductViewPo query) {
        log.info("分页查询产品列表: current={}, size={}, query={}", current, size, query);
        Page<ProductViewPo> page = new Page<>(current, size);
        return productApplicationService.list(page, query);
    }

    /**
     * 根据ID查询产品详情
     */
    @GetMapping("/{productId}")
    public R<ProductViewPo> getById(@PathVariable Long productId) {
        log.info("查询产品详情: productId={}", productId);
        return productApplicationService.getById(productId);
    }

    /**
     * 根据状态查询产品列表
     */
    @GetMapping("/status/{status}")
    public R<Page<ProductViewPo>> getByStatus(@PathVariable String status,
                                              @RequestParam(defaultValue = "1") int current,
                                              @RequestParam(defaultValue = "10") int size) {
        log.info("根据状态查询产品列表: status={}, current={}, size={}", status, current, size);
        Page<ProductViewPo> page = new Page<>(current, size);
        return productApplicationService.getByStatus(page, status);
    }

    /**
     * 根据产品ID获取BOM信息
     */
    @GetMapping("/{productId}/bom")
    public R<Object> getBOMByProductId(@PathVariable Long productId) {
        log.info("查询产品BOM信息: productId={}", productId);
        return productApplicationService.getBOMByProductId(productId);
    }

    /**
     * @description 获取可用产品
     * @date 2025/10/19
     * @author dxy
     */
    @GetMapping("/availableProduct")
    public R<List<ProductVo>> getAvailableProduct() {
        List<ProductVo> productVoList = productApplicationService.getAvailableProduct();
        return R.ok(productVoList, "查询成功");
    }
}